﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using SGLib.Scene;
using PBMessage;
public class GameScene : BaseScene
{
    public long ServerTime { get; set; }
    public long PreServerTime { get; set; }
    public long ServerDT { get; set; }

    Dictionary<long, Unit> mUnits = new Dictionary<long, Unit>();
    Unit mMainPlayer;
    public Unit MainUnit
    {
        get { return mMainPlayer; }
    } 
    GameCamera mGameCamera;
    public GameCamera GameCamera { get{ return mGameCamera; } }
    GSPackageHandler mGSPackageHandler = new GSPackageHandler();

    SC_EnterGameRoom cacheData;
    public override void OnInit(params object[] args)
    {
        cacheData = args[0] as SC_EnterGameRoom ;
        this.SceneLoader = new NormalSceneLoader("GameMap");
    }
    public override void OnLoad()
    {
        Game.Inst.Net.SetHandler(PBMesasgeID.SC_CreateUnit, onCreateUnit);
        Game.Inst.Net.SetHandler(PBMesasgeID.SC_UnitState, onStateChange);
        Game.Inst.Net.SetHandler(PBMesasgeID.SC_SyncUnitProperty,OnUnitSyncProperty);
        mGSPackageHandler.Start(this);
        mGameCamera  = GameObject.Find("Main Camera").GetComponent<GameCamera>();
        UIManager.Inst.Show("UIBattleView");
        EffectManager.Instance.Load();
        AbilityManager.Instance.Load();

        this.OnAddMapItem(cacheData);
    }
    public Unit GetUnit(long unitId)
    {
        return mUnits[unitId];
    }

    public void RemoveUnit(long unitId)
    {
        mUnits.Remove(unitId);
    }
    void onCreateUnit(PBPackage pkg)
    {
        SC_CreateUnit createUnit = (SC_CreateUnit)pkg.Data;
        //自己
        if (createUnit.PlayerId == PlayerModel.Instance.Self.GUID)
        {
          Unit newUnit =  Unit.Create((Hashtable)Game.Inst.Table.UnitTable[createUnit.UnitId],createUnit.PlayerId);
          mUnits.Add(newUnit.GUID,newUnit);
          mGameCamera.LookAt(newUnit.transform);
          mMainPlayer = newUnit;
          ((UIBattleView)UIManager.Inst.Get("UIBattleView")).SetUnitAbilityBar();
        }
        //其他人
        else
        {
            Unit newUnit = Unit.Create((Hashtable)Game.Inst.Table.UnitTable[createUnit.UnitId], createUnit.PlayerId);
            mUnits.Add(newUnit.GUID, newUnit);
            newUnit.transform.LookAt(new Vector3(0,0,1));
            newUnit.transform.position = new Vector3(createUnit.PosX / 100f, 0, createUnit.PosY/100f);
        }
    }

 
    void onStateChange(PBPackage pkg)
    {   
        SC_UnitState state = (SC_UnitState)pkg.Data;
        Unit unit = mUnits[state.UnitId];
        if (unit.State == state.State)
        {
            return;
        }
        if (unit.State == 2 && state.State != 2)
        {
            unit.Event.CallEvent("Unit.Ability.End",unit.CurAbility.Name);
        }
        unit.State = state.State;
        switch (state.State)
        {
            case 0:
                unit.Event.CallEvent("Unit.StateChange", "Stand");
            break;
            case 1:
                unit.Event.CallEvent("Unit.StateChange", "Move");
                break;
            case 2:
                unit.Event.CallEvent("Unit.StateChange", "Ability",state.StringP0);
                unit.CurAbility = unit.Abilitys[state.StringP0];
                break;
            case 3:
                unit.Event.CallEvent("Unit.StateChange","Die");
                break;
            case 4:
                GameObject.Destroy(unit.gameObject);
                break;
            case 5:
                unit.Event.CallEvent("Unit.StateChange", "HitFly");
                unit.HitFly(state.Time,state.IntP0,state.IntP1,state.IntP2, state.IntP3);
                break;
        }
    }

    void OnUnitSyncProperty(PBPackage pkg)
    {
        SC_SyncUnitProperty pkgData = (SC_SyncUnitProperty)pkg.Data;
        Unit curUnit =  this.mUnits[pkgData.UnitId];
        if (pkgData.HPSpecified)
        {
            curUnit.SetProperty(":HP", pkgData.HP);
        }
        if (pkgData.StaminaSpecified)
        {
            curUnit.SetProperty(":Stamina", pkgData.Stamina);
        }
    }

    void OnAddMapItem(SC_EnterGameRoom data)
    {
        for (int i = 0; i < data.ItemId.Count; i++)
        {
           GameObject itemG = GameObject.CreatePrimitive(PrimitiveType.Sphere);
           itemG.transform.localPosition = new Vector3(data.PosX[i]/100f,0.25f, data.PosZ[i] / 100f);
           itemG.transform.localScale *= 0.5f;
        }
    }

    public override void OnUpdate()
    {
        if (Input.GetMouseButtonDown(1))
        {
            RaycastHit hitInfo;
            var Ray = mGameCamera.Camera.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(Ray, out hitInfo))
            {
                Vector3 dir = hitInfo.point - mMainPlayer.transform.position;
                mMainPlayer.MoverController.OnClientMover(1, new Vector3(dir.x, 0, dir.z),true,hitInfo.point);
            }
        }
        base.OnUpdate();
    }

    public void ServerUpdate(long dt)
    {
        foreach (Unit unit in mUnits.Values)
        {
            unit.ServerUpdate(dt);
        }
    }
}
